<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '修改物流' : '修改物流'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="订单号" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单号" disabled="true"></el-input>
    </el-form-item> -->
    <el-form-item label="物流公司" prop="companyId">
      <el-select v-model="dataForm.companyName" placeholder="物流公司" @change="sele">
          <el-option
            v-for="item in dataForm.list"
            :key="item.id"
            :label="item.companyName"
            :value="item.id">
          </el-option>
        </el-select>
      <!-- <el-input v-model="dataForm.deletestatus" placeholder=""></el-input> -->
    </el-form-item>
    <el-form-item label="物流单号" prop="deliveryNo">
      <el-input v-model="dataForm.deliveryNo" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          ecId: '',
          deliveryNo: '',
          list: [],
          companyId: '',
          companyName: ''
        },
        dataRule: {
          companyId: [
            { required: true, message: '物流公司不能为空', trigger: 'blur' }
          ]
          // ,
          // deliveryNo: [
          //   { pattern: /^[0-9]+.?[0-9]*$/, message: '物流单号为纯数字', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id, ecId, deliveryNo) {
        this.dataForm.deliveryNo = deliveryNo
        this.dataForm.id = id
        this.dataForm.ecId = ecId
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shopping/order/getExpressNameByCartId/${ecId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.companyId = data.data.id
                this.dataForm.companyName = data.data.companyName
              }
            })
          }
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shopping/order/queryLogisticsCompany`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.list = data.data
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shopping/order/modifyExpressNameOrDeliveryNoById`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'companyId': this.dataForm.companyId,
                'shipCode': this.dataForm.deliveryNo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      sele (id) {
        this.dataForm.companyId = id
      }
    }
  }
</script>
